package com.algorithm_leaning;


import lombok.Data;
import org.junit.Test;

@Data
class LinkedNode{
    private int value;
    private LinkedNode next;

    public LinkedNode(int value) {
        this.value = value;
    }

    public LinkedNode(int value, LinkedNode next) {
        this.value = value;
        this.next = next;
    }
}

public class LinkedList {
    // 指定node插入数据
    public void insert(int index, int value, LinkedNode head) {
        int step = 0;
        LinkedNode currentNode = head;
        while (currentNode != null) {
            if (step == index) {
                LinkedNode newNode = new LinkedNode(value, currentNode.getNext());
                currentNode.setNext(newNode);
            }
            currentNode = currentNode.getNext();
            step++;
        }
    }

    // 删除指定位置的数据
    public void delete(int index, LinkedNode head ) {
        if (index == 0) {
            head = head.getNext();
            return;
        }

        LinkedNode currentNode = head;
        int step = 0;
        while (currentNode != null) {
            if (step == index) {
                currentNode = currentNode.getNext();
            }
            currentNode = currentNode.getNext();
            step++;
        }
    }

    public void append(int value, LinkedNode head){
        LinkedNode currentNode = head;
        while (currentNode.getNext() != null){
            currentNode = currentNode.getNext();
        }
        currentNode.setNext(new LinkedNode(value));
    }

    @Test
    public void test() {
        LinkedNode linkedNode = new LinkedNode(11);
        for (int i = 0; i < 10; i++) {
            LinkedNode linkedNode1 = new LinkedNode(i, linkedNode);
            linkedNode = linkedNode1;
        }
    }

}


